package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions",regionService.getHotRegions());
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(Model model,Long regionId){
        //根据regionid来查热门目的地
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        //将查询出的list分成两份
        int size = list.size();
        //左边[0,size/2)
        model.addAttribute("leftDests",list.subList(0,size / 2));
        //右边[size/2,size)
        model.addAttribute("rightDests",list.subList(size / 2,size));
        //拼接html模板
        model.addAttribute("regionId",regionId);

        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        //获取id相对应的
        List<Destination> toasts = destinationService.getToasts(id);
        //移除索引为最后一层的
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        //攻略分类,根据目的地的id来分类
        model.addAttribute("catalogs", strategyCatalogService.queryCatalogByDestId(id));
        //前3篇,
        List<StrategyDetail> top3 = strategyDetailService.getViewNumTop3(id);
        model.addAttribute("strategyDetails",top3);
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String guide(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        //获取id相对应的
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //移除索引为最后一层的
        Destination dest = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",dest);
        return "destination/survey";
    }


    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        //某个目的地下的分类集合
        List<StrategyCatalog> strategyCatalogs = strategyCatalogService.queryCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",strategyCatalogs);
        //选中的分类
        StrategyCatalog strategyCatalog = strategyCatalogService.get(qo.getCatalogId());
        model.addAttribute("catalog",strategyCatalog);
        //选中的分类的第一个明细,以及其内容
        List<StrategyDetail> details = strategyCatalog.getDetails();
        if (details!=null&&details.size()>0){
            StrategyDetail strategyDetail = details.get(0);
            StrategyContent content = strategyDetailService.getContent(strategyDetail.getId());
            strategyDetail.setStrategyContent(content);
            model.addAttribute("detail",strategyDetail);
        }
        return "destination/surveyTpl";
    }

    //某个目的地下的游记列表
    @RequestMapping("/travels")
    public String travelPage(Model model, @ModelAttribute("qo") TravelQuery qo){
        model.addAttribute("pageInfo",travelService.query(qo));
        return "destination/travelTpl";
    }


}
